<?php

ignore_user_abort(true); // 后台运行，不受前端断开连接影响
set_time_limit(0);
ini_set('max_execution_time', '0');
chdir(dirname(__FILE__));
// error_reporting(E_ERROR);

require_once ("../../log.php");
require_once ("../../config.php");
require_once ("core.php");
require_once ("../api_tool.php");

$ic_list = AllQuery("SELECT * FROM item_category WHERE channel_id = 3 AND status = '1';");

foreach ($ic_list as $ic_value) {
	$resp = getPddDdkGoodsSearch($ic_value['category_id']);
	if (empty($resp['goods_list'])) {
		 continue;
	}
 	foreach ($resp['goods_list'] as $map_data) {
		$detail_images = '';
		$price = $map_data['min_group_price'];
		$coupon_amount = $map_data['has_coupon']?$map_data['coupon_discount']:'';
		$coupon_start_fee = $map_data['has_coupon']?$map_data['coupon_min_order_amount']:'';

		$price = $price / 100;
		$coupon_amount = $coupon_amount / 100;
		$coupon_start_fee = $coupon_start_fee / 100;

		$parent_category_ids = ',0,';
		if (!empty($ic_value['parent_category_ids'])) {
			$parent_category_ids .= "{$ic_value['parent_category_ids']},";
		}
		var_dump($map_data);
		if ($coupon_amount == 0) {
			if (mt_rand(1, 100) > 10) {
				break;
			}
		}
		$good_list = array (
			'item_id'				=> $map_data['goods_id'],			// 商品ID
			'name'					=> $map_data['goods_name'],			// 商品名称
			'title'					=> $map_data['goods_desc'],			// 商品标题
			'image' 				=> $map_data['goods_image_url'],	// 商品图片
			'url'					=> '',								// 商品图片
			'price' 				=> $price,							// 商品价格
			'detail_images'			=> $detail_images,					// 展示图册
			'commission_rate' 		=> $map_data['promotion_rate'],		// 佣金比率
			'coupon_url' 			=> '',								// 领券链接
			'coupon_amount' 		=> $coupon_amount,					// 优惠券面值
			'coupon_start_fee' 		=> $coupon_start_fee,				// 优惠券起用金额
			'channel_id' 			=> 3,								// 渠道代码:1=淘宝, 2=京东, 3=拼多多
			'category_id' 			=> $map_data['cat_ids'][0],			// 商品类别ID
			'category_name' 		=> '',								// 商品类别
			'sub_category_id' 		=> $map_data['category_id'],		// 商品子类别ID
			'sub_category_name' 	=> $map_data['category_name'],		// 商品子类别
			'nick' 					=> $map_data['mall_name'],			// 店铺昵称
			'parent_category_ids'	=> $parent_category_ids,			// 主目录id
		);

		$t_key = "";
		$t_value = "";
		foreach ($good_list as $key => $value) {
			$t_key .= $key;
			$t_value .= "'{$value}'";
			end($good_list);
			if ($key !== key($good_list)) {
				$t_key .= ', ';
				$t_value .= ', ';
			}
		}
		simQuery("INSERT INTO item_ordinary ({$t_key}) VALUES ({$t_value})");
	}
}

?>